package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.entity.DtGoods;
import com.dk.entity.DtOrder;
import com.dk.entity.Evaluate;
import com.dk.entity.TbAddress;
import com.dk.service.OrderServiceI;
import com.dk.util.IdWorker;
import com.dk.util.JwtUtil;
import com.netflix.zuul.context.RequestContext;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("transaction")
@Api("详情页点击生成订单")
public class OrderController {

    @Autowired
    private OrderServiceI orderServiceI;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    @ResponseBody
    @ApiOperation(value = "点击我想要, 传递商品id, 根据商品id查询出数据, 跳转到聊天页发送给卖家")
    public Result getGoodsByGoodsId(Long goodsId){
        DtGoods goods = orderServiceI.selectByGoodsId(goodsId);
        return Result.ok().setData(goods);
    }

    @GetMapping("getAddress|")
    @ResponseBody
    @ApiOperation(value = "点击购买时, 确定地址,前台展示地址, 根据用户id查询地址")
    public Result getAddress(HttpServletRequest request){
//        //请求上下文
//        RequestContext currentContext = RequestContext.getCurrentContext();
////      获取request对象
//        HttpServletRequest request = currentContext.getRequest();
////
//        String authToken = request.getHeader("authToken");
//        //JWT网关
//
//        Claims claims = jwtUtil.parseJwt(authToken);
//        String id = claims.getId();// 用户id

        List<TbAddress> tbAddress = orderServiceI.selectAddressByUserId("1");

        return Result.ok().setData(tbAddress);
    }


    @PostMapping("createOrder")
    @ResponseBody
    @ApiOperation(value = "上海生成订单表, 将用户信息和商品信息传递")
    public Result createOrder(DtOrder dtOrder){
        long l = idWorker.nextId();
        dtOrder.setPaymentorderno(String.valueOf(l));
        dtOrder.setStatus("1");
        dtOrder.setCreateTime(new Date());
        dtOrder.setUpdateTime(new Date());
        orderServiceI.insertOrder(dtOrder);
        return Result.ok();
    }

    @PostMapping("commentAdd")
    @ResponseBody
    @ApiOperation("增加评论, 添加评论内容 , 先判断是否收货 ")
    public Result commentAdd (Evaluate evaluate , Long orderId){
//        判断是否已经确认收货 , 根据orderId查询订单状态
        DtOrder dtOrder = orderServiceI.selectOrderId(orderId);
//        if (dtOrder.getStatus() != "5"){
//            return Result.ok().setData("请确认收货后再评价");
//        }
        evaluate.setId(idWorker.nextId());
        evaluate.setDate(new Date());
        Map map = new HashMap();
        map.put("orderId" , orderId);
        map.put("grade",evaluate.getGrade());
        String s = JSONObject.toJSONString(map);
        Message message = MessageBuilder.withPayload(s).build();
//                                                                                             事务消息所在的组          topic         消费所需要的消息  发送者所需要的数据(半消息)
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("tx-group", "order_topic", message, evaluate);

        return Result.ok();
    }
}
